#import "Common.h"
#import "DataType.h"
#import "Devourer.h"
#import "Particle.h"

@implementation Devourer

@synthesize isShieldOn = mShieldOn;

-(id)init
{
	if ((self = [super init])) {
		mShieldOn = false;

		mDevourer = [CCSprite spriteWithFile:@"devourer.png"];
		mDevourer.position = CGPointZero;
		[self addChild:mDevourer];

		mRadius = 10;

		[mDevourer runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:1 angle:-360]]];

		mShield = [CCSprite spriteWithFile:@"shield.png"];
		mShield.visible = mShieldOn;
		[self addChild:mShield z:15];

		mEmitter = [ParticleDevourer node];
		mEmitter.position = CGPointZero;
		[self addChild:mEmitter z:-1];
	}
	return self;
}

-(void)dealloc
{
	[super dealloc];
}
	
-(bool)containPoint:(CGPoint)pos
{
	return ccpDistance(self.position,pos) <= mRadius*3;
}

-(void)enableShield
{
	mRadius = 15;
	mShieldOn = YES;
	mShield.visible = mShieldOn;
}

-(void)disableShield
{
	mRadius = 10;
	mShieldOn = NO;
	mShield.visible = mShieldOn;
}

-(void)setDevourerPos:(CGPoint)pos
{
	if(pos.x < gSettingData.startPosX)
		pos.x = gSettingData.startPosX;
	else if(pos.x > 420 + gSettingData.startPosX)
		pos.x = 420 + gSettingData.startPosX;
	
	if(pos.y < 0)
		pos.y = 0;
	else if(pos.y > 320)
		pos.y = 320;
	
	self.position = pos;
}

-(bool)checkCollision:(InvaderBase*)ib
{
	return ccpDistance(self.position,ib.position) <= mRadius+ib.radius;
}

@end
